import Qt 4.7

Item {
    id: authorizationUI

    signal authorized
    signal cancel

    width: parent.width
    height: parent.height

    // Note:
    // the followings should be dialog common settings
    // * to close dialog
    // * disable "close-dialog" for dialog area

    // to close dialog
    MouseArea {
        x: 0
        y: 0
        width: parent.width
        height: parent.height
        onClicked: {
            authorizationUI.cancel()
            authorizationUI.state = "startAuthorization"
        }
    }
    // disable "close-dialog" for dialog area
    MouseArea {
        width: main.width
        height: main.height
        anchors.horizontalCenter: main.horizontalCenter
    }

    Rectangle {
        id: main
        width: 300
        height: 200
        anchors.horizontalCenter: parent.horizontalCenter
        color: "#ff0000"

        // UI: Start authorization
        Item {
            id: startAuthorization
            anchors.fill: parent
            opacity: 1

            Button {
                id: authorizeButton
                y: 10
                width: 240
                height: 32
                text: "Authorize account"
                anchors.horizontalCenter: parent.horizontalCenter

                onClicked: {
                    authorizationUI.state = "sendVerifierCode"
                    var requestToken = meetup.getRequestToken();
                    var url = "http://www.meetup.com/authorize/?oauth_token=" + requestToken;
                    Qt.openUrlExternally(url);
                }
            }

            Text {
                id: information
                text: "Click 'Authorization' button"
                anchors.horizontalCenter: parent.horizontalCenter
                font.pointSize: fontSize
                color: "#ffffff"
                y: 100
            }
        }

        // UI: Send verifier code and get access token/ sercret
        Item {
            id: verification
            anchors.fill: parent
            opacity: 0

            Input {
                id: verifierCode
                anchors.horizontalCenter: parent.horizontalCenter
                y: 50
                width: 140
                height: 32
                //onAccepted: { Qt.openUrlExternally("http://google.com") }
            }
            Button {
                id: sendVerifierCode
                y: 10
                text: "send verifier code"
                width: 240
                height: 32
                anchors.horizontalCenter: parent.horizontalCenter
                onClicked: {
                    var code = verifierCode.text
                    var result = meetup.sendVerifierCode(code);
                    //if (result == true)
                    authorizationUI.authorized();
                }
            }
            Text {
                text: "Web browser shows verifier code,\nplease input the code"
                anchors.horizontalCenter: parent.horizontalCenter
                font.pointSize: fontSize
                color: "#ffffff"
                y: 100
            }
        }
    }

    states: [
        State {
            name: "startAuthorization"
            PropertyChanges {
                target: startAuthorization
                opacity: 1
            }
            PropertyChanges {
                target: verification
                opacity: 0
            }
        },
        State {
            name: "sendVerifierCode"
            PropertyChanges {
                target: startAuthorization
                opacity: 0
            }
            PropertyChanges {
                target: verification
                opacity: 1
            }
       }
    ]
}
